package com.xbq.exceleditor.ui

import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ResourceUtils
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ktx.immersionBar
import com.xbq.exceleditor.R
import com.xbq.exceleditor.databinding.ActivityTemplateDetailBinding
import com.xbq.exceleditor.utils.ensureVipClick
import com.xbq.exceleditor.utils.getTemplateDir
import com.xbq.xbqsdk.component.activity.VBActivity
import com.xbq.xbqsdk.util.ShareFileUtils
import com.xbq.xbqsdk.util.click.onDebouncedClick
import dagger.hilt.android.AndroidEntryPoint
import java.io.File


/**
 * Author: liaohaiping
 * Time: 2020-10-30
 * Description: 快捷键
 */
@AndroidEntryPoint
class TemplateDetailActivity : VBActivity<ActivityTemplateDetailBinding>() {
    companion object {
        fun start(doc_thumnail: String) {
            val bundle = Bundle().apply { putString("doc_thumbnail", doc_thumnail) }
            ActivityUtils.startActivity(bundle, TemplateDetailActivity::class.java)
        }
    }

    val docThumbnail by lazy { intent.getStringExtra("doc_thumbnail") ?: "" }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            titleBar(R.id.statusbar)
            statusBarDarkFont(true)
        }
        binding.btnBack.onDebouncedClick { finish() }
        binding.btnExport.ensureVipClick(this, "exportDocument") { exportFile() }
        createTbsReaderView()
        viewFile()

    }

    fun createTbsReaderView() {
//        mTbsReaderView = TbsReaderView(this, this)

        //rl_tbsView为装载TbsReaderView的视图
//        binding.pdfContainer.addView(mTbsReaderView, RelativeLayout.LayoutParams(
//            ViewGroup.LayoutParams.MATCH_PARENT,
//            ViewGroup.LayoutParams.MATCH_PARENT))
    }

    fun viewFile() {
        val pair = ensureCopyFile()
        val thumbnailFile = pair.first
        val docFile = pair.second
//        val bundle = Bundle()
//        bundle.putString("filePath", docFile.absolutePath)
//        bundle.putString("tempPath", PathUtils.getTempDir(this).absolutePath)
//        val result: Boolean = mTbsReaderView.preOpen(docFile.extension, false)
//        if (result) {
//            mTbsReaderView.openFile(bundle)
//        }

        Glide.with(this)
            .load(thumbnailFile)
            .into(binding.docImg)
    }

    fun exportFile() {
        val pair = ensureCopyFile()
        ShareFileUtils.shareFile(this, pair.second.absolutePath)
    }


    private fun ensureCopyFile(): Pair<File, File> {
        val templateDir = getTemplateDir()
        var thumbnailFile = File(templateDir, docThumbnail)
        if (thumbnailFile.exists() == false) {
            ResourceUtils.copyFileFromAssets("jianli/${docThumbnail}", thumbnailFile.absolutePath)
        }
        var doc = docThumbnail.replace(".jpg", ".xlsx")
        val docFile = File(templateDir, doc)
        if (!docFile.exists()) {
            ResourceUtils.copyFileFromAssets("jianli/${doc}", docFile.absolutePath)
        }

        return Pair(thumbnailFile, docFile)
    }


    override fun onDestroy() {
        super.onDestroy()
    }


}